<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/17
 * Time: 17:06
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class ProductRemoveParameter extends BaseValidate
{
    protected $rule=[
        'product'=>'require|checkProducts',
        'category_id'=>'require|isPositiveInt',
    ];

    protected $singleRule=[
        'id'=>'require|isPositiveInt',
    ];


    protected  $message=[
        'product'=>'删除规格的数组不能为空',
        'category_id'=>'分类ID参数不正确'
    ];

    public function checkProducts($values){
        if (empty($values)){
            throw new ParameterException([
                'msg'=>'商品不能为空'
            ]);
        }
        if (!is_array($values)){
            throw new ParameterException([
                'msg'=>'商品参数不正确'
            ]);
        }
        foreach ($values as $value){
            $this->checkProduct($value);
        }
        return true;
    }

    public function checkProduct($value){
        $validate  = new BaseValidate($this->singleRule);
        $result = $validate->check($value);
        if (!$result){
            throw new ParameterException([
                'msg'=>'商品参数不正确'
            ]);
        }
        return true;
    }
}